﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class NhanVien
    {

        public int MaNV;
        public string MaNVHienThi
        {
            get
            {
                return "NV" + MaNV;
            }
        }
        public string TenNV { get; set; }
        public int MaPB { get; set; }
        public string TenPB { get; set; }
        public string ChucVu { get; set; }

        public string DiaChi { get; set; }

        public string DienThoai { get; set; }
        public DateTime NgaySinh { get; set; }

        public bool TheoDoi { get; set; }
        public string TenDangNhap { get; set; }
        public string MatKhau { get; set; }

        public int Quyen { get; set; }



        //load
        public NhanVien(int maNV, string tenNV, int maPB, string tenPB, string chucVu,
            string diaChi, string dienThoai, DateTime ngaySinh, bool theoDoi)
        {
            this.MaNV = maNV;
            this.TenNV = tenNV;
            this.MaPB = maPB;
            this.TenPB = tenPB;

            this.ChucVu = chucVu;
            this.DiaChi = diaChi;

            this.NgaySinh = ngaySinh;
            this.DienThoai = dienThoai;
            this.TheoDoi = theoDoi;
        }


        public NhanVien(string maNVHienThi, string tennv, int maPb, string chucVu,
             string diaChi, string dienThoai, DateTime ngaySinh, bool theoDoi)
        {
            this.MaNV = Int16.Parse(maNVHienThi.Substring(2));
            this.TenNV = tennv;

            this.MaPB = maPb;
            this.ChucVu = chucVu;
            this.DiaChi = diaChi;

            this.DienThoai = dienThoai;

            this.NgaySinh = ngaySinh;
            this.TheoDoi = theoDoi;
        }

        //them moi
        public NhanVien(string tennv, int maPb, string chucVu,
            string diaChi, string dienThoai, DateTime ngaySinh, bool theoDoi)
        {
            this.TenNV = tennv;
            this.MaPB = maPb;
            this.ChucVu = chucVu
                ;
            this.DiaChi = diaChi;

            this.DienThoai = dienThoai;
            this.NgaySinh = ngaySinh;
            this.TheoDoi = theoDoi;
        }

        public NhanVien(int maNV, int quyen)
        {
            this.MaNV = maNV;
            this.Quyen = quyen;
        }

       
        public static NhanVien DangNhap(string tendangnhap, string matkhau)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.NhanViens
                    where p.TenDangNhap == tendangnhap &&
                           p.MatKhau == matkhau
                    select new NhanVien(p.MaNV, p.Quyen)).Single();
        }

        public static bool DoiMatKhau(string tendangnhap, string matKhauCu, string matKhauMoi)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            try
            {
                var nhanVien = (from p in db.NhanViens
                                where p.TenDangNhap == tendangnhap
                                select p).Single();
                if (nhanVien.MatKhau == matKhauCu && nhanVien.TenDangNhap==tendangnhap)
                {
                    nhanVien.MatKhau = matKhauMoi;
                    db.SubmitChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch 
            {
                return false;
            }
        }

        public static void ThemMoi(NhanVien nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.NhanVien tamObject = new DataAccess.NhanVien();

            tamObject.TenNV = nhapObject.TenNV;
            tamObject.MaPB = nhapObject.MaPB;
            tamObject.ChucVu = nhapObject.ChucVu;
            tamObject.DiaChi = nhapObject.DiaChi;

            tamObject.DienThoai = nhapObject.DienThoai;
            tamObject.NgaySinh = nhapObject.NgaySinh;
            tamObject.TheoDoi = nhapObject.TheoDoi;

            db.NhanViens.InsertOnSubmit(tamObject);
            db.SubmitChanges();
        }

        public static void CapNhat(NhanVien nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var tamObject = (from p in db.NhanViens
                             where p.MaNV == nhapObject.MaNV
                             select p).Single();
            tamObject.TenNV = nhapObject.TenNV;
            tamObject.MaPB = nhapObject.MaPB;
            tamObject.ChucVu = nhapObject.ChucVu;
            tamObject.DiaChi = nhapObject.DiaChi;

            tamObject.DienThoai = nhapObject.DienThoai;
            tamObject.NgaySinh = nhapObject.NgaySinh;
            tamObject.TheoDoi = nhapObject.TheoDoi;
            db.SubmitChanges();
        }



        public static List<NhanVien> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            return (from p in db.NhanViens
                    select new NhanVien(p.MaNV, p.TenNV, p.MaPB, p.PhongBan.TenPB, p.ChucVu, p.DiaChi, p.DienThoai, p.NgaySinh, p.TheoDoi)).ToList();
        }

        public static List<NhanVien> LayDanhSachNVKD()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            return (from p in db.NhanViens
                    where p.MaPB == 2
                    select new NhanVien(p.MaNV, p.TenNV, p.MaPB, p.PhongBan.TenPB, p.ChucVu, p.DiaChi, p.DienThoai, p.NgaySinh, p.TheoDoi)).ToList();
        }

    }
}
